package com.example.springstudy.controller;

import com.example.springstudy.base.Result;
import com.example.springstudy.dao.dto.OrderDTO;
import com.example.springstudy.entity.Order;
import com.example.springstudy.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/api/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @PostMapping("/create")
    public Result<Order> createOrder(@RequestBody OrderDTO order) {
        try {
            log.info("order: {}", order);
            Assert.isTrue(order.getShopId()!=0, "shopId must not be 0");
            Assert.isTrue(order.getEmployeeId()!=0, "EmployeeId must not be 0");
            Assert.notEmpty(order.getSkuList(), "商品不能为空");
            return Result.success(orderService.createOrder(order));
        } catch (Exception e) {
            return Result.failed(e.getMessage());
        }
    }

    @GetMapping("/pay")
    public Result<Boolean> payOrder(@RequestParam String barCode, @RequestParam String orderNo, @RequestParam String amount) {
        try{
            log.info("barCode:{}, orderNo:{} amount:{}", barCode, orderNo, amount);
            Assert.hasText(barCode, "barCode must not be empty");
            Assert.hasText(orderNo, "orderNo must not be empty");
            Assert.hasText(amount, "amount must not be empty");
            return Result.success(orderService.orderPay(barCode, orderNo, amount));
        } catch (Exception e) {
            return Result.failed(e.getMessage());
        }
    }
}